<?php
// +----------------------------------------------------------------------
// | 十分彩网系统 [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace Wechat\Controller;
use Common\Controller\HomebaseController; 
/**
 * 首页
 */
class OrderController extends HomebaseController {
	var $_AdminName="admin123";
	var $_AdminId="1";
	public function addPayRecord($vars,$pay_arr=array()){
		if(empty($vars['order_sn'])){
			if($vars['type'] == "1"){
				$paytype = M('paytype')->where(array("id"=>$vars['paytype'],"status"=>"1"))->find();
				$pay['pay_type']=$paytype['id'];
				$pay['pay_typename']=$paytype['name'];
				$pay['is_mobile']=$paytype['is_mobile'];
			}
			$pay['pay_sn']=$this->getOrderSn();//获取唯一订单号
			$pay['member_id']=$pay_arr['mid'];
			$pay['member_name']=$pay_arr['muser'];
			$pay['pay_money']=$pay_arr['content_money'];
			$pay['content_type']=$vars['type'];//支付的类型
			$pay['package_id']=$vars['packid'];
			$pay['pay_name']=$pay['pay_sn'];
			$pay['is_pay']=10;//状态默认为未支付
			$pay['create_time']=date("Y-m-d H:i:s");
			$item = M('payorder');
			$id = $item->max("id");
			$pay['id'] = $id + 1;
			if ($item->add($pay)!==false) {
				return $pay;
			}else{
				$this->error("数据错误，请重新下单",U('user/profile/buycard'));
			}
		}else{
			$item = M('payorder')->where(array("pay_sn"=>$vars['order_sn']))->find();
			return $item;
		}
	}
	
	public function addPayOrderRecord($vars){
		if(!empty($vars)){
			$num=M('payorder')->where(array("pay_sn"=>$vars['order_sn']))->count();
			if($num>0){
				p("已有支付记录为开发");
			}else{
				$tmp_order = M("order")->where(array("content_sn"=>$vars['order_sn'],"status"=>"1"))->find();
				if(!empty($tmp_order)){
					$record = array ('status' => "302",'msg' => '您要支付的订单不存在！');
				}
				if($tmp_order['is_pay'] == "2" && $tmp_order['order_shop'] == "1"){
					$frm=$this->getAddOrderPayFields($tmp_order,$vars);
					$frm['content_type']=$vars['type'];
					$item = M('payorder');
					if ($item->add($frm)!==false) {
						$_tmporder['content_money']=$tmp_order['total_price'];
						$_tmporder['order_title']=$vars["order_sn"];
						$_tmporder['content_sn']=$vars["order_sn"];
						$_record=array('status'=>'300','data'=>$_tmporder);
					}else{
						$record = array ('status' => "304",'msg' => '数据错误，请重新下单！');
					}
				}else{
					$record = array ('status' => "303",'msg' => '您的订单已支付！');
				}
			}
		}else{
			$record = array ('status' => "301",'msg' => '未获得订单号，请重新下单！');
		}
		return $record;
	}
	
	public function getAddOrderPayFields($field,$_varsmem){
		$id = M('payorder')->max('id');
		$paytype = M('paytype')->where(array("id"=>$field['paytype']))->find();
		$frm['id'] = $id+1;
		$frm['pay_type'] = $field['paytype'];
		$frm['pay_typename'] = $paytype['name'];
		$frm['pay_sn']  = $field['content_sn'];
		$frm['member_id'] = $_varsmem['member_id'];
		$frm['member_name'] = $_varsmem['member_name'];
		$frm['pay_money'] =  $field['total_price'];
		$frm['pay_name']  = $field['content_sn'];
		$frm['create_time'] = date("Y-m-d H:i:s");
		$frm['is_mobile'] = "2";
		$frm['order_time'] = $field['create_time'];
		return $frm;
	}
	
	public function PaySuccessReturn($order_sn,$money,$paytype,$trade_no){//修改支付记录里的支付方式
		$payo = M('payorder')->where(array('pay_sn'=>$order_sn));
		$payorder = $payo->select();
		if(!empty($payorder) && is_array($payorder)){
			$p_money='';
			$p_pack=",";
			foreach($payorder as $key=>$item){
				$p_pack .=$item['package_id'].",";
				$p_money +=$item['pay_money'];
				if($item["is_pay"]==20){
					return false;
				}
			}
			if($p_money!=$money){
				return false;
			}else{
				$user = M("Jinshun_agent",null)->where(array("AGENTID"=>$payorder['0']['member_id']))->find();
				if(!empty($user) && is_array($user)){
					//获取卡数
					$pack= trim($p_pack,',');
					$package = M("package")->field("packprice,packcount,givecount")->where(array("packid"=>$pack))->find();
					$sale['G_GLODNUMBER']=$package['packcount']+$package['givecount'];
					//添加购卡记录
					$sale['G_COMMENT']="微信支付";
					$sale['G_STATE']="1";
					$sale['G_AGENTID'] = $user['a_name'];
					$sale['G_IP'] =	get_real_ip();
					$agent['A_WORKTIME'] = $sale['G_TIME'] = date("Y-m-d H:i:s");
					$sale['G_CHIEF'] = $this->_AdminName;
					$agent['A_TEL'] = $sale['G_GLODNUMBER']+$user['a_tel'];
					if(M("Jinshun_glodwholesale",null)->add($sale) !== false){
						$saleid = M("Jinshun_glodwholesale",null)->max("glodwholesaleid");
						$pay["sale_id"]=$saleid;
						$pay["is_pay"]=20;
						$pay["pay_type"]=$paytype;
						$pay["pay_price"]=$money;
						$pay["trade_no"]=$trade_no;
						$pay["update_time"]=date("Y-m-d H:i:s");
						if (M('payorder')->where(array('pay_sn'=>$order_sn))->save($pay)!==false) {
							M("Jinshun_agent",null)->where(array("AGENTID"=>$user['agentid']))->save($agent);
							//修改session
							session('user.a_tel',$agent['A_TEL']);
							session('user.a_worktime',$agent['A_WORKTIME']);
							return true;
						}else {
							return false;
						}
					}
				}else{
					return false;
				}
			}	
		}else{
			return false;
		}
	}
	public function getOrderSn(){ // 生成充值记录的订单号
		$yCode = array(
				'H',
				'A',
				'I',
				'B',
				'O',
				'Y',
				'C'
		);
		$order_sn = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), - 5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
		return $order_sn;
	}
}
?>